package com.ruoyi.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.Rejectsn;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.domain.bo.QualityRejectsnLossesBo;
import com.ruoyi.quality.domain.vo.QualityRejectsnLossesVo;
import com.ruoyi.quality.domain.QualityRejectsnLosses;
import com.ruoyi.quality.mapper.QualityRejectsnLossesMapper;
import com.ruoyi.quality.service.IQualityRejectsnLossesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 不合格品损失Service业务层处理
 *
 * @author yanjun
 * @date 2023-09-12
 */
@RequiredArgsConstructor
@Service
public class QualityRejectsnLossesServiceImpl implements IQualityRejectsnLossesService {

    private final QualityRejectsnLossesMapper baseMapper;

    /**
     * 查询不合格品损失
     */
    @Override
    public QualityRejectsnLossesVo queryById(Long rejectsnId){
        return baseMapper.selectVoById(rejectsnId);
    }

    /**
     * 查询不合格品损失
     */
    @Override
    public QualityRejectsnLossesVo queryByOddNumber(String oddNumber){
        QualityRejectsnLossesBo bo = new QualityRejectsnLossesBo();
        bo.setOddNumber(oddNumber);
        LambdaQueryWrapper<QualityRejectsnLosses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);

    }

    /**
     * 查询不合格品损失
     */
    @Override
    public List<QualityRejectsnLossesVo> queryByIds(Collection<Long> rejectsnIds){
        return baseMapper.selectVoBatchIds(rejectsnIds);
    }

    /**
     * 查询不合格品损失列表
     */
    @Override
    public TableDataInfo<QualityRejectsnLossesVo> queryPageList(QualityRejectsnLossesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QualityRejectsnLosses> lqw = buildQueryWrapper(bo);
        Page<QualityRejectsnLossesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不合格品损失列表
     */
    @Override
    public TableDataInfo<QualityRejectsnLossesVo> queryPageListSelect(QualityRejectsnLossesBo bo, PageQuery pageQuery) {

        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        List<QualityRejectsnLossesVo> result = queryList(bo).stream().filter(s->s.getHandlingConclusion().equals(Rejectsn.Repair) || s.getHandlingConclusion().equals(Rejectsn.Rework)).filter(s->!s.getDeptAbbreviation().equals(Rejectsn.DEPT_DEFAULT)).collect(Collectors.toList());
        int num = result.size();
        result = result.stream()
                .skip((long) (pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setRows(result);
        tableDataInfo.setTotal(num);
        return tableDataInfo;
    }

    /**
     * 查询不合格品损失列表
     */
    @Override
    public List<QualityRejectsnLossesVo> queryList(QualityRejectsnLossesBo bo) {
        LambdaQueryWrapper<QualityRejectsnLosses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QualityRejectsnLosses> buildQueryWrapper(QualityRejectsnLossesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QualityRejectsnLosses> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDiscoveryUnit()), QualityRejectsnLosses::getDiscoveryUnit, bo.getDiscoveryUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsibilityUnit()), QualityRejectsnLosses::getResponsibilityUnit, bo.getResponsibilityUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getOddNumber()), QualityRejectsnLosses::getOddNumber, bo.getOddNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderUnit()), QualityRejectsnLosses::getOrderUnit, bo.getOrderUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptAbbreviation()), QualityRejectsnLosses::getDeptAbbreviation, bo.getDeptAbbreviation());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNumber()), QualityRejectsnLosses::getContractNumber, bo.getContractNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getPartNumber()), QualityRejectsnLosses::getPartNumber, bo.getPartNumber());
        lqw.like(StringUtils.isNotBlank(bo.getProjectNumber()), QualityRejectsnLosses::getProjectNumber, bo.getProjectNumber());
        lqw.like(StringUtils.isNotBlank(bo.getPartName()), QualityRejectsnLosses::getPartName, bo.getPartName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterial()), QualityRejectsnLosses::getMaterial, bo.getMaterial());
        lqw.eq(bo.getWeight() != null, QualityRejectsnLosses::getWeight, bo.getWeight());
        lqw.eq(bo.getWorkingHours() != null, QualityRejectsnLosses::getWorkingHours, bo.getWorkingHours());
        lqw.eq(bo.getUnitPrice() != null, QualityRejectsnLosses::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getLossAmount() != null, QualityRejectsnLosses::getLossAmount, bo.getLossAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getSeverityLevel()), QualityRejectsnLosses::getSeverityLevel, bo.getSeverityLevel());
        lqw.eq(bo.getNumber() != null, QualityRejectsnLosses::getNumber, bo.getNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getInspectionStatus()), QualityRejectsnLosses::getInspectionStatus, bo.getInspectionStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlingSuggestion()), QualityRejectsnLosses::getHandlingSuggestion, bo.getHandlingSuggestion());
        lqw.eq(StringUtils.isNotBlank(bo.getReasonCode()), QualityRejectsnLosses::getReasonCode, bo.getReasonCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDefectCode()), QualityRejectsnLosses::getDefectCode, bo.getDefectCode());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlingConclusion()), QualityRejectsnLosses::getHandlingConclusion, bo.getHandlingConclusion());
        lqw.between(params.get("beginRegistrationTime") != null && params.get("endRegistrationTime") != null,
            QualityRejectsnLosses::getRegistrationTime ,params.get("beginRegistrationTime"), params.get("endRegistrationTime"));
        return lqw;
    }

    /**
     * 新增不合格品损失
     */
    @Override
    public Boolean insertByBo(QualityRejectsnLossesBo bo) {
        QualityRejectsnLosses add = BeanUtil.toBean(bo, QualityRejectsnLosses.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRejectsnId(add.getRejectsnId());
        }
        return flag;
    }

    /**
     * 修改不合格品损失
     */
    @Override
    public Boolean updateByBo(QualityRejectsnLossesBo bo) {
        QualityRejectsnLosses update = BeanUtil.toBean(bo, QualityRejectsnLosses.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QualityRejectsnLosses entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除不合格品损失
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
